/*
 * @(#)Id3Tag.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.model.media;

import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.farng.mp3.AbstractMP3Tag;
import org.farng.mp3.MP3File;

/**
 * <code>Id3Tag</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.3 $
 * @since 0.1
 */
public class Id3Tag implements Tag {

	private static final Log log = LogFactory.getLog(Id3Tag.class);

	private String album;

	private String artist;

	private String comment;

	private String genre;

	private String title;

	private int totalTracks;

	private int trackNumber;

	private int year;

	private Id3Tag() throws IOException {
		super();
	}

	public static Id3Tag[] getId3Tags(File[] sources) throws IOException {
		final Id3Tag[] tags = new Id3Tag[sources.length];
		for (int i = 0; i < sources.length; i++) {
			tags[i] = getId3Tag(sources[i]);
		}

		return tags;
	}

	public static Id3Tag getId3Tag(File source) {
		try {
			MP3File mp3file = new MP3File(source);
			return getId3Tag(mp3file);
		}
		catch (Exception e) {
			log.warn(e.toString() + " [" + source + "]");
		}
		return null;
	}

	public static Id3Tag getId3Tag(MP3File mp3file) throws IOException {
		final Id3Tag tag = new Id3Tag();

		AbstractMP3Tag mp3tag = null;
		if (!mp3file.hasFilenameTag()) {
			throw new IOException("mp3file[" + mp3file.getMp3file().getName() + " doesn't have filename tag");
		}

		if (mp3file.hasID3v2Tag()) {
			mp3tag = mp3file.getID3v2Tag();
		}
		else if (mp3file.hasID3v1Tag()) {
			mp3tag = mp3file.getID3v1Tag();
		}
		else if (mp3file.hasLyrics3Tag()) {
			mp3tag = mp3file.getLyrics3Tag();
		}
		else {
			throw new IOException("mp3file[" + mp3file.getMp3file().getName() + " doesn't have a tag");
		}

		tag.setAlbum(mp3tag.getAlbumTitle());
		tag.setArtist(mp3tag.getLeadArtist());
		tag.setComment(mp3tag.getSongComment());
		tag.setGenre(mp3tag.getSongGenre());
		tag.setTitle(mp3tag.getSongTitle());

		try {
			// tag.trackNumber = new
			// Byte(mp3tag.getTrackNumberOnAlbum()).shortValue();
			tag.setYear(Short.parseShort(mp3tag.getYearReleased()));
		}
		catch (NumberFormatException ne) {
		}

		return tag;
	}

	/**
	 * @return Returns the album.
	 */
	public String getAlbum() {
		return this.album;
	}

	/**
	 * @param album The album to set.
	 */
	public void setAlbum(String album) {
		this.album = album;
	}

	/**
	 * @return Returns the artist.
	 */
	public String getArtist() {
		return this.artist;
	}

	/**
	 * @param artist The artist to set.
	 */
	public void setArtist(String artist) {
		this.artist = artist;
	}

	/**
	 * @return Returns the comment.
	 */
	public String getComment() {
		return this.comment;
	}

	/**
	 * @param comment The comment to set.
	 */
	public void setComment(String comment) {
		this.comment = comment;
	}

	/**
	 * @return Returns the genre.
	 */
	public String getGenre() {
		return this.genre;
	}

	/**
	 * @param genre The genre to set.
	 */
	public void setGenre(String genre) {
		this.genre = genre;
	}

	/**
	 * @return Returns the title.
	 */
	public String getTitle() {
		return this.title;
	}

	/**
	 * @param title The title to set.
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return Returns the totalTracks.
	 */
	public int getTotalTracks() {
		return this.totalTracks;
	}

	/**
	 * @param totalTrack The totalTracks to set.
	 */
	public void setTotalTracks(int totalTracks) {
		this.totalTracks = totalTracks;
	}

	/**
	 * @return Returns the trackNumber.
	 */
	public int getTrackNumber() {
		return this.trackNumber;
	}

	/**
	 * @param trackNumber The trackNumber to set.
	 */
	public void setTrackNumber(int trackNumber) {
		this.trackNumber = trackNumber;
	}

	/**
	 * @return Returns the year.
	 */
	public int getYear() {
		return this.year;
	}

	/**
	 * @param year The year to set.
	 */
	public void setYear(int year) {
		this.year = year;
	}
}